<template>
  <section class="cms-body flex">
    <!-- 返回组件 -->
    <a class="cms-back" href="javascript:void(0)" @click="back"></a>
    <div>
      <el-form ref="form" :model="info" :rules="rules" class="cms-form" label-width="162px" v-loading="loading">
        <!-- 动态表单 -->
        <!-- 频道 -->
        <el-form-item label="选择频道" class="flex-50" prop="parentId">
          <el-cascader class="cms-width" :props="{'value':'id','label':'name','children':'child'}"
                       :options="channelList" v-model="info.parentId" filterable v-if="dataState" :disabled="true">
          </el-cascader>
          <span class="gray">只能选择最后一层</span>
        </el-form-item>

        <!-- 关联专题 -->
        <el-form-item label="关联专题" class="flex-50" prop="topicId" v-if="params.parentId!=111">
          <el-select class="cms-width" filterable v-model="info.topicId" clearable>
            <el-option v-for="(item,index) in topicList" :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联标签" class="flex-50">
          <el-select class="cms-width" filterable v-model="info['tagId']" clearable>
            <el-option v-for="(tag,index) in tagList" :key="index" :label="tag.tagName" :value="tag.tagId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联公仔纸作者" class="flex-50" prop="authorUserId" v-if="params.parentId==111">
          <el-select class="cms-width" v-model="info['authorUserId']" filterable>
            <el-option v-for="(item,index) in userList" :key="index" :label="item.penName"
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联财经专栏作者" class="flex-50" prop="authorUserId" v-if="params.parentId==129">
          <el-select class="cms-width" v-model="info['authorUserId']" filterable>
            <el-option v-for="(item,index) in userList" :key="index" :label="item.penName"
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联文化专栏作者" class="flex-50" prop="authorUserId" v-if="params.parentId==156">
          <el-select class="cms-width" v-model="info['authorUserId']" filterable>
            <el-option v-for="(item,index) in userList" :key="index" :label="item.penName"
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item class="flex-100" label="meta关键词">
          <el-input class="cms-width" v-model="info['metaKeyword']"></el-input>
        </el-form-item>

        <el-form-item class="flex-100" label="meta描述">
          <el-input class="cms-width" v-model="info['metaDesc']"></el-input>
        </el-form-item>

        <el-form-item v-for="(item,index) in itemList" :key="index" v-if="item.field=='title'" class="flex-100"
                      :label="item.label" prop="title">
          <el-input class="cms-width" v-model="info['title']" placeholder="100字以内"></el-input>
          <!--   <el-checkbox v-model="isLink">外部链接</el-checkbox>-->
        </el-form-item>
        <!-- 系统字段 -->
        <el-form-item v-for="(item,index) in itemList"
                      :key="index" :label="item.label"
                      :class="[item.single?'flex-100':'flex-50']"
                      :prop="(item.custom?'attr_':'')+item.field"
                      :rules="item.required?fieldRequied:[]"
                      v-if="item.field!='channelId'&&item.field!='title'&&item.field!='typeImg'&&!(item.field=='titleImg'&&!hasTitleImg)&&!(item.field=='contentImg'&&!hasContentImg)">
          <el-input class="cms-width" v-model="info['attr_'+item.field]"
                    v-if="item.dataType==1&&item.custom"></el-input>
          <el-input class="cms-width" v-model="info[item.field]" v-if="item.dataType==1
                &&!item.custom
                &&item.field!='titleImg'
                &&item.field!='contentImg'
                &&item.field!='typeImg'
                &&item.field!='attachments'
                &&item.field!='media'
                &&item.field!='name'
                &&item.field!='origin'">
          </el-input>
          <!-- 动态提示 -->
          <div class="inline-block" v-if="item.field=='origin'">
            <el-input v-model="info[item.field]" class="inline-block"></el-input>
            <div class="content-inner-box">
              <div style="margin-top:15px;margin-bottom: 15px;width: 600px">
                <label class="ml-24 inner-label">来源地址</label>
                <el-input v-model="info['originUrl']" style="width: 400px"></el-input>
              </div>
            </div>
          </div>
          <el-input v-if="item.field=='name'" class="cms-width" v-model="info[item.field]" @blur="getPath"></el-input>
          <!-- 标题，内容图片 -->
          <cms-upload :src="info[item.field]" v-if="item.field=='titleImg'&&hasTitleImg" :isMark="false"
                      @change="getTitleImg"></cms-upload>
          <cms-upload :src="info[item.field]" v-if="item.field=='contentImg'&&hasContentImg" :isMark="false"
                      @change="getContentImg"></cms-upload>
          <!-- 附件 -->
          <cms-multiple-upload v-if="item.field=='attachments'&&dataState" @change="getAttachments"
                               :propList="info.attachments"></cms-multiple-upload>
          <!--文档路径  -->
          <cms-attach-upload :src="info['docPath']" v-if="item.field=='docPath'"
                             action='/api/member/upload/o_upload_doc' :multiple="false" :isDoc="true"
                             @change="getDocPath"></cms-attach-upload>
          <!-- 图片集 -->
          <cms-pictrues v-if="item.field=='pictures'&&dataState" @change="getPics" :propList="info.pics"></cms-pictrues>
          <!-- 整形文本 -->
          <el-input class="cms-width" v-model.number="info['attr_'+item.field]" v-if="item.dataType==2&&item.custom"
                    type="number" min='0'></el-input>
          <el-input class="cms-width" v-model="info[item.field]" v-if="item.dataType==2&&!item.custom" type="number"
                    min='0'></el-input>
          <!-- 浮点形文本 -->
          <el-input class="cms-width" v-model="info['attr_'+item.field]"
                    v-if="item.dataType==3&&item.custom"></el-input>
          <!-- 文本区 -->
          <el-input class="cms-width" v-model="info['attr_'+item.field]" type="textarea"
                    v-if="item.dataType==4&&item.custom">
          </el-input>
          <div v-if="item.dataType==4&&!item.custom&&item.field!='txt'
                 &&item.field!='txt1' &&item.field!='txt2'&&item.field!='txt3'
                 &&item.field!='docPath' &&item.field!='pictures'">
            <el-input class="cms-width" v-model="info[item.field]" type="textarea" v-if="item.field=='description'"
                      placeholder="500字以内" maxlength="500"></el-input>

            <el-input class="cms-width" v-model="info[item.field]" type="textarea" v-else></el-input>
          </div>
          <!-- 日期 -->
          <el-date-picker class="cms-width" v-model="info['attr_'+item.field]" type="date" value-format="yyyy-MM-dd"
                          v-if="item.dataType==5&&item.custom"></el-date-picker>
          <el-date-picker class="cms-width" v-model="info[item.field]" type="date" value-format="yyyy-MM-dd"
                          v-if="item.dataType==5&&!item.custom&&item.field!='releaseDate'"></el-date-picker>
          <!-- 发布时间，归档日期 -->
          <div v-if="item.field=='releaseDate'">
            <el-date-picker v-model="info['releaseDate']" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                            style="width:195px">
            </el-date-picker>
          </div>
          <!-- 下拉列表 -->
          <el-select class="cms-width" v-model="info['attr_'+item.field]" v-if="item.dataType==6&&item.custom">
            <el-option v-for="(opt,optIndex) in item.optValue" :key="optIndex" :label="opt" :value="opt">
            </el-option>
          </el-select>
          <!-- 标题颜色 -->
          <div v-if="item.field=='titleColor'" class="flex">
            <el-color-picker v-model="info[item.field]"></el-color-picker>
            <el-checkbox v-model="info['bold']" class="ml-24">是否加粗</el-checkbox>
          </div>
          <!-- 固定级别 -->
          <div v-if="item.field=='topLevel'">
            <el-input class="w50" type="number" v-model="info['topLevel']"></el-input>
          </div>
          <!--内容类型-->
          <div v-if="item.field=='typeId'">
            <el-select style="width:220px" v-model="info['typeId']" @change="getTypeId">
              <el-option v-for="(item,index) in typeList" :key="index" :label="item.name" :value="item.id"
                         :disabled="true"></el-option>
            </el-select>
            <div v-if="isTypeImg" class="bread-box" style="margin-top:12px;">
              <label for="" style="margin-right:10px;">上传封面</label>
              <cms-upload :src="info['typeImg']" field="typeImg" :isMark="false" :action="attchUploadUrl" :upLoadWay="1"
                          :fileRemark="info.title"           @get="getFieldImg" uploadPath="images" :isShowAttachment="true" attachmentFileType="picture"
                          @getAttachmentVal="getAttachmentVal" :channelId="attchmentChannelId"></cms-upload>
            </div>
            <p v-if="isTypeImg" style="color:red; margin-top: 8px;">(建议封面上传图片尺寸比例：16:9；格式：jpg、jpeg、png)</p>
            <el-radio-group v-model="uploadVideoWay" v-if="info.typeId==2" style="margin-top: 10px;">
              <el-radio :label="1">上传视频</el-radio>
              <el-radio :label="2">填写路径</el-radio>
            </el-radio-group>
            <div v-if="info.typeId==2 && uploadVideoWay==1" class="bread-box" style="margin-top:12px;">
              <label for="" style="margin-right:10px;">阿里云视频点播videoId</label>
              <el-input type="text" style="width: 200px" v-model="info['videoId']"></el-input>
              <el-input type="text" style="width: 200px" v-model="info.mediaPath=''" v-show="false"></el-input>
              <ali-video-upload :videoId="info['videoId']" field="videoId" @get="getVideoPath" uploadPath="video"
                                :isShowAttachment="true" attachmentFileType="demandVideo" :action="attchUploadUrl" :upLoadWay="1"
                                :fileRemark="info.title"                 :channelId="attchmentChannelId"></ali-video-upload>
            </div>
            <div v-if="info.typeId==2 && uploadVideoWay==2" class="bread-box" style="margin-top:12px;">
              <label for="" style="margin-right:10px;">网络地址</label>
              <el-input type="text" style="width: 650px" v-model="info['mediaPath']"></el-input>
              <el-input type="text" style="width: 200px" v-model="info.videoId=''" v-show="false"></el-input>
              <div style="margin-left: 8px;">
                <cms-attachment attachmentFileType="ossVideo" field="mediaPath"
                                @getAttachmentVal="getAttachmentVal" :channelId="attchmentChannelId"></cms-attachment>
              </div>
            </div>
            <div v-if="info.typeId==1" class="bread-box" style="margin-top:12px;">
              <label for="" style="margin-right:10px;">上传视频</label>
              <cms-video-upload :src="info['mediaPath']" field="mediaPath" @get="getFieldImg"
                                uploadPath="video" :channelId="attchmentChannelId":action="attchUploadUrl" :upLoadWay="1" :fileRemark="info.title"></cms-video-upload>
            </div>
            <div v-if="info.typeId==1" class="bread-box" style="margin-top:12px;">
              <label for="" style="margin-right:10px;">视频路径</label>
              <el-input v-model="info['mediaPath']" type="text" style="margin-top:10px;width:70%"></el-input>
              <div style="margin-top: 8px;margin-left: 8px;">
                <cms-attachment attachmentFileType="ossVideo" field="mediaPath" @getAttachmentVal="getAttachmentVal"
                                :channelId="attchmentChannelId"></cms-attachment>
              </div>

            </div>
          </div>
          <!-- 频道模版 -->
          <el-select class="cms-width" v-model="info[item.field]" v-if="item.field=='tplContent'">
            <el-option label="默认" value="/default/content/news.html"></el-option>
            <el-option v-for="(item,index) in tplAll.contentTpl" :key="index" :label="item" :value="item"></el-option>
          </el-select>
          <!-- 频道手机模板 -->
          <el-select class="cms-width" v-model="info[item.field]" v-if="item.field=='tplMobileContent'">
            <el-option label="默认" value=""></el-option>
            <el-option v-for="(item,index) in tplAll.contentMobileTpl" :key="index" :label="item"
                       :value="item"></el-option>
          </el-select>
          <!-- 复选框 -->
          <!-- 自定义字段拼接名称-->
          <el-checkbox-group v-model="info['attr_'+item.field]" v-if="item.dataType==7&&item.custom">
            <el-checkbox v-for="(opt,optIndex) in item.optValue" :key="optIndex" :label="opt">

            </el-checkbox>
          </el-checkbox-group>
          <!-- 浏览权限 -->
          <el-checkbox-group v-model="info[item.field]"
                             v-if="item.dataType==7&&!item.custom&&item.field=='viewGroupIds'">
            <el-checkbox v-for="(item,index) in groupList" :key="index" :label="item.id">{{item.name}}</el-checkbox>
          </el-checkbox-group>
          <!-- 单选框 -->
          <el-radio-group v-model="info['attr_'+item.field]" v-if="item.dataType==8&&item.custom">
            <el-radio :label="opt" v-for="(opt,optIndex) in item.optValue" :key="optIndex">
            </el-radio>
          </el-radio-group>
          <!-- 是否开放 -->
          <el-radio-group v-model="info['isOpen']" v-if="item.field=='isOpen'">
            <el-radio :label="true">
              是
            </el-radio>
            <el-radio :label="false">
              否
            </el-radio>
          </el-radio-group>
          <div v-if="item.field=='charge'">
          </div>
          <!-- 自定义图片-->
          <cms-multiple-upload :src="info['attr_'+item.field]" :multiple="false" :field="'attr_'+item.field"
                               @get="getvideoId" btnLabel="上传"
                               v-if="item.dataType==9&&item.custom"></cms-multiple-upload>
          <cms-upload :src="info['attr_'+item.field]" :field="'attr_'+item.field" @get="getFieldImg"
                      v-if="item.dataType==10&&item.custom"></cms-upload>
          <!--富文本编辑器-->
          <div v-if="item.field=='txt'&& dataState">
            <!--  <cms-export-upload :index="0" @get="getDocTxt"></cms-export-upload>-->
            <cms-editor :defaultMsg="info['txt']" :index="0" @ready="getUeditor"></cms-editor>
          </div>
          <div v-if="item.field=='txt1'&&dataState">
            <!--  <cms-export-upload :index="1" @get="getDocTxt"></cms-export-upload>-->
            <cms-editor :defaultMsg="info['txt1']" :index="1" @ready="getUeditor"></cms-editor>
          </div>
          <div v-if="item.field=='txt2'&&dataState">
            <!--   <cms-export-upload :index="2" @get="getDocTxt"></cms-export-upload>-->
            <cms-editor :defaultMsg="info['txt2']" :index="2" @ready="getUeditor"></cms-editor>
          </div>
          <div v-if="item.field=='txt3'&&dataState">
            <!--  <cms-export-upload :index="3" @get="getDocTxt"></cms-export-upload>-->
            <cms-editor :defaultMsg="info['txt3']" :index="3" @ready="getUeditor"></cms-editor>
          </div>
        </el-form-item>
        <el-form-item class="flex-100" label="内容状态" v-show="false">
          <el-radio-group v-model="info['status']" :disabled="true">
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">送签</el-radio>
            <el-radio :label="2">已发布</el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 动态表单 -->
        <div class="form-footer">
          <el-button type="primary" @click="update(false)" v-perms="'/content/toSign/update'">修改</el-button>
          <el-button type="primary" @click="lookInfo(info.id)">预览</el-button>
        </div>
      </el-form>
      <el-dialog title="文章预览" :visible.sync="isShowContent" v-if="isShowContent">
        <content-preview :contentId="previewId"></content-preview>
      </el-dialog>
      <content-top></content-top>
    </div>
  </section>
</template>
<script>
  import formMixins from "@/mixins/form";
  import axios from "axios";
  import va from "@/rules";

  export default {
    mixins: [formMixins],
    data() {
      let required = va.required("此项必填");
      let number = va.number("只能输入数字");
      return {
        params: this.$route.query, //前一个页面带过来的数据
        rules: {
          parentId: [required],
          title: [required],
          typeId: [required, number],
          isOpen: [number],
          charge: [number]
        },
        fieldRequied: [
          {required: true, message: "此项必填", trigger: "change"}
        ],
        parentId: "",
        dataState: false,
        isLink: false, //是否开启外部链接
        hasContentImg: false,
        hasTitleImg: false,
        channelList: [], //频道列表
        itemList: [], //动态列表
        topicList: [], //专题列表
        typeList: [], //内容类型列表
        groupList: [], //用户组列表
        workflowList: [], //工作流
        tplAll: [], //所有模型
        contentInfo: {},
        info: {
          parentId: "", //上层频道id,
          channelId: "", //频道id
          bold: false,
          topicId: "", //专题id
          link: "", //外部链接地址
          originUrl: "", //来源地址
          topLevelDate: "",
          sortDate: "",
          releaseDate: "",
          pigeonholeDate: "",
          typeId: "",
          recommend: false,
          recommendLevel: 1,
          draft: false,
          tplContent: "",
          tplMobileContent: "",
          charge: false,
          attachments: [],
          attachmentNames: [], //附件名称
          attachmentPaths: [], //附件地址
          videoId: "", //媒体路径
          mediaType: "CK", //媒体类型
          mediaPath: '',
          pics: [],
          picPaths: [], //图片地址
          picDescs: [], //图片描述
          chargeAmount: "", //总费
          rewardPattern: false, //奖励模式
          rewardFix: "", //固定奖
          rewardRandomMin: 0, //随机奖励最小值
          rewardRandomMax: 10, //随机奖励最大值
          docPath: "", //doc地址
          docFile: "", //文档名称
          downNeed: 0, //下载需要财富值
          isOpen: false //是否开启
        },
        isTypeImg: false,
        props: {
          label: "name",
          children: "child",
          isLeaf: "hasChild",
          id: "id"
        },
        ue: [],
        tagList: [],
        uploadVideoWay: 1,
        userList: [],
        isShowContent: false,
        previewId: '',
        isNoSaveLeave: true,
        changeCount: 0,
        attchmentChannelId: '',
        attchUploadUrl:this.$api.attachmentBatchUpload
      };
    },
    methods: {
      lookInfo(id) {
        this.isShowContent = true;
        this.previewId = id;
      },
      back() {
        let params = this.$route.query.params;
        this.routerLink("/content/toSign/list", "list", "", {params: params});
      },
      getTypeId(val) {
        axios.post(this.$api.typeGet, {id: val}).then(res => {
          if (res.code == "200") {
            this.isTypeImg = res.body.hasImage;
          }
        });
      },
      getUserList(parentId) {
        let params = {}
        if (parentId == 111) {
          params = {departId: 2};
        }
        if (parentId == 129) {
          params = {departId: 4};
        }
        if (parentId == 156) {
          params = {departId: 5};
        }
        axios.post(this.$api.userList, params).then(res => {
          if (res.code == "200") {
            let userList = res.body;
            this.userList = userList;
          }
        });
      },
      getPath(event) {
        if (event.target.value != "") {
          axios.post(this.$api.channelCreatPath, {name: event.target.value}).then(res => {
            this.info.path = res.body;
          });
        }
      },
      getFieldImg(path, field, index, pindex) {
        this.info[field] = path;
      },
      getVideoPath(path) {
        this.info.videoId = path;
      },
      getAttachments(res) {
        this.info.attachments = res; //附件对象数组
      },
      getAttachmentVal(result) {
        let field = result.field;
        let fieldVal = result.fieldVal;
        this.info[field] = fieldVal;
        console.log(this.info)
      },
      getPics(res) {
        //图片
        this.info.pics = res; //附件对象数组
      },
      getDocPath(res) {
        //文档路径
        this.info.docPath = res.uploadPath; //文档路径
        this.info.docFile = res.fileName;
      },
      getDocTxt(txt, index) {
        this.ue[index].execCommand("inserthtml", txt);
      },
      getvideoId(path, field) {
        //动态媒体路径
        this.info[field] = path; //媒体路径
      },
      getUeditor(ue, index) {
        this.ue[index] = ue; //获取ue实例
      },
      getTitleImg(path) {
        //标题图
        this.info.titleImg = path;
      },
      getContentImg(path) {
        //内容图
        this.info.contentImg = path;
      },
      getTypeImg(path) {
        //类型图
        this.info.typeImg = path;
      },
      getContentInfo(id) {
        axios.post(this.$api.contentGet, {id: id}).then(res => {
          let data = res.body;
          console.log(res);
          this.info = data;
          let mediaPath = this.info.mediaPath;
          if (!this.$myUtils.isNull(mediaPath)) {
            this.uploadVideoWay = 2;
          }
          this.contentInfo = data;
          this.params.modelId = res.body.modelId;
          this.info.modelId = res.body.modelId;
          this.info.typeId = res.body.typeId;
          this.params.parentId = res.body.channelId;
          this.parentId = res.body.channelId;
          this.getDataInfo();
        });
        this.loading = false;
      },
      getDataInfo() {
        this.loading = true;
        //先获取模型id
        let modelParams = {modelId: this.params.modelId, isChannel: false};
        axios.post(this.$api.itemList, modelParams).then(res => {
          this.loading = false;
          //this.$refs["form"].resetFields();//移除校验结果并重置字段值
          let itemList = res.body; //渲染数据字段模型
          for (let i in itemList) {
            if (itemList[i].custom) {
              //判断是否是系统字段
              if (itemList[i].dataType != 7) {
                this.$set(this.info, "attr_" + itemList[i].field, this.contentInfo["attr_" + itemList[i].field]);
              }
            } else {
              if (itemList[i].dataType == 7) {
                this.$set(this.info, itemList[i].field, this.contentInfo[itemList[i].field]);
              } else {
                this.$set(this.info, itemList[i].field, this.contentInfo[itemList[i].field]);
              }
            }
          }
          this.itemList = itemList;
          this.getAllList(); //获取辅助字段列表
          this.getDefaultInfo(); //数据回填
        }).catch(err => {
          this.loading = false;
        });
      },
      getDefaultInfo() {
        //数据回填
        this.info.bold = this.contentInfo.bold;
        this.info.originUrl = this.contentInfo.originUrl;
        this.info.sortDate = this.contentInfo.sortDate;
        this.info.topLevelDate = this.contentInfo.topLevelDate;
        this.info.pigeonholeDate = this.contentInfo.pigeonholeDate;
        this.info.viewGroupIds = this.contentInfo.viewGroupIds;
        this.info.typeId = this.contentInfo.typeId;
        this.info.downNeed = this.contentInfo.downNeed;
        this.info.isOpen = this.contentInfo.isOpen;
        this.info.recommend = this.contentInfo.recommend;
        this.info.recommendLevel = this.contentInfo.recommendLevel;
        this.info.draft = this.contentInfo.draft;
        this.info.topicId = this.contentInfo.topicId;
        this.info.videoId = this.contentInfo.videoId;
        this.info.mediaType = this.contentInfo.mediaType;
        this.info.chargeAmount = this.contentInfo.chargeAmount;

        this.$set(this.info, "rewardPattern", this.contentInfo.rewardPattern);
        this.$set(this.info, "rewardRandomMax", this.contentInfo.rewardRandomMax);
        this.$set(this.info, "rewardRandomMin", this.contentInfo.rewardRandomMin);
        this.getTypeId(this.info.typeId);
        this.getUserList(this.params.parentId);
        this.$set(this.info, "attachments", []);
        this.$set(this.info, "pics", []);
        if (this.contentInfo.link != "") {
          this.isLink = true;
          this.info.link = this.contentInfo.link;
        }
        if (this.contentInfo.isOpen == "") {
          //是否开放
          this.info.isOpen = true;
        }
        if (this.contentInfo.attachArr.length > 0) {
          for (let i in this.contentInfo.attachArr) {
            let obj = {
              name: this.contentInfo.attachArr[i].attachmentNames,
              path: this.contentInfo.attachArr[i].attachmentPaths
            };
            this.info.attachments.push(obj);
          }
        }
        if (this.contentInfo.picArr.length > 0) {
          for (let j in this.contentInfo.picArr) {
            let obj = {
              name: this.contentInfo.picArr[j].picDescs,
              path: this.contentInfo.picArr[j].picPaths
            };
            this.info.pics.push(obj);
          }
        }
        this.dataState = true;
      },
      getAllList() {
        let api = this.$api; //API地址
        axios.post(api.channelGet, {id: this.params.parentId}).then(res => {
          this.hasContentImg = res.body.hasContentImg;
          this.hasTitleImg = res.body.hasTitleImg;
          if (this.params.parentId != "") {
            //频道数据回填
            this.info.parentId = res.body.nodeIds; //层级id[]
          } else {
            this.info.parentId = [""]; //层级id[]
          }
        });
        axios.all([
          axios.post(api.contentTagList, {}),
          axios.post(api.fullTextSearchChannelList, {hasContentOnly: true}), //频道列表
          axios.post(api.topicListAll, {channelId: this.params.parentId}), //关联专题列表
          axios.post(api.typeList, {containDisabled: false}), //内容类型列表
          axios.post(api.groupList), //会员组列表
          axios.post(api.tplModelList, {modelId: this.params.modelId})
        ]).then(
          axios.spread((tagRes, channels, topicList, type, groups, tplAll) => {
            this.tagList = tagRes.body; //所有模型
            this.channelList = this.channelList.concat(channels.body); //频道列表
            this.topicList = topicList.body; //关联专题列表
            this.typeList = type.body; //内容类型列表
            this.groupList = groups.body; //会员组列表
            this.tplAll = tplAll.body; //所有模型
          })
        ).catch(err => {
          this.loading = false;
        });
      },
      getEditorContent() {
        //处理一下频道的富文本内容,固定只有四个
        if (this.info.txt != undefined && this.ue[0]) {
          this.info.txt = this.ue[0].getContent();
        }
        if (this.info.txt1 != undefined && this.ue[1]) {
          this.info.txt1 = this.ue[1].getContent();
        }
        if (this.info.txt2 != undefined && this.ue[2]) {
          this.info.txt2 = this.ue[2].getContent();
        }
        if (this.info.txt3 != undefined && this.ue[3]) {
          this.info.txt3 = this.ue[3].getContent();
        }
      },
      getParams() {
        //获取到处理完成给后台提交的对象
        let params = {}; //数组对象换成字符串
        let picPaths = [];
        let picDescs = [];
        let attachmentNames = [];
        let attachmentPaths = [];
        this.getEditorContent();
        for (let j = 0; j < this.info.pics.length; j++) {
          //处理图集
          picPaths.push(this.info.pics[j].path);
          picDescs.push(this.info.pics[j].name);
        }
        for (let i in this.info.attachments) {
          //处理附件
          attachmentNames.push(this.info.attachments[i].name);
          attachmentPaths.push(this.info.attachments[i].path);
        }
        this.info.picPaths = picPaths;
        this.info.picDescs = picDescs;
        this.info.attachmentNames = attachmentNames;
        this.info.attachmentPaths = attachmentPaths;

        for (let key in this.info) {
          if (this.info[key] instanceof Array) {
            params[key] = this.info[key].join(",");
          } else {
            params[key] = this.info[key];
          }
        }
        delete params.attachments;
        delete params.pics;
        delete params.comments;
        params.channelId = params.parentId.substring(params.parentId.lastIndexOf(",") + 1);
        params.id = this.params.id;
        return params;
      },
      reset() {
        window.location.reload();
      },
      showCheckResult(title) {
        this.$confirm(title, '警告', {type: 'error', showCancelButton: false, showConfirmButton: false},).then(mes => {
        }).catch(error => {
          this.loading = false
        })
      },
      checkFields(params) {
        console.log(params)
        let msg = {};
        if (this.$myUtils.isNull(params.topicId) && params.channelId == 2) {
          msg['result'] = false;
          msg['info'] = '请选择专题';
          return msg;
        }
        if (this.$myUtils.isNull(params.authorUserId) && params.channelId == 111) {
          msg['result'] = false;
          msg['info'] = '请选择公仔纸作者';
          return msg;
        }
        if (this.$myUtils.isNull(params.authorUserId) && params.channelId == 129) {
          msg['result'] = false;
          msg['info'] = '请选择专栏作者';
          return msg;
        }
        if (this.$myUtils.isNull(params.authorUserId) && params.channelId == 156) {
          msg['result'] = false;
          msg['info'] = '请选择专栏作者';
          return msg;
        }
        if (this.$myUtils.isNull(params.title)) {
          msg['result'] = false;
          msg['info'] = '请填写文章标题';
          return msg;
        }
        if (this.$myUtils.isNull(params.description)) {
          msg['result'] = false;
          msg['info'] = '请填写摘要';
          return msg;
        } else {
          if (params.description.length > 500) {
            msg['result'] = false;
            msg['info'] = '摘要最多500字';
            return msg;
          }
        }
        if (this.$myUtils.isNull(params.typeImg)) {
          msg['result'] = false;
          msg['info'] = '请上传封面图片';
          return msg;
        }
        let typeId = params.typeId;
        if (typeId == 1) {
          if (this.$myUtils.isNull(params.txt)) {
            msg['result'] = false;
            msg['info'] = '请填写内容';
            return msg;
          }
        }
        if (typeId == 2) {
          if (this.uploadVideoWay == 1) {
            if (this.$myUtils.isNull(params.videoId)) {
              msg['result'] = false;
              msg['info'] = '请上传视频';
              return msg;
            }
          }
          if (this.uploadVideoWay == 2) {
            if (this.$myUtils.isNull(params.mediaPath)) {
              msg['result'] = false;
              msg['info'] = '请上传视频';
              return msg;
            }
          }
        }
        msg['result'] = true;
        msg['info'] = '校验成功';
        return msg;
      },
      update(state) {
        this.isNoSaveLeave = false;
        //修改频道
        let params = this.getParams();
        console.log(params)
        let msg = this.checkFields(params);
        if (!msg.result) {
          this.showCheckResult(msg.info);
          return false;
        }
        //验证方法
        this.loading = true;
        axios.post(this.$api.contentUpdate, this.getParams()).then(res => {
          if (res.code == "200") {
            this.successMessage("修改成功");
            setTimeout(() => {
              let params = this.$route.query.params;
              this.routerLink("/content/toSign/list", "list", "", {params: params});
            }, 1000);
            this.loading = false;
          }
        }).catch(error => {
          this.loading = false;
        });
      }
    },
    created() {
      this.$store.dispatch("setCollapse", false); //收缩状态栏
      let channelLevel = this.$route.query.channelLevel;
      let channelId = this.$route.query.channelId;
      let parentId = this.$route.query.parentId;
      if (channelLevel == 1) {
        this.attchmentChannelId = Number(channelId);
      }
      if (channelLevel == 2) {
        this.attchmentChannelId = Number(parentId);
      }
      console.log(this.attchmentChannelId)
      this.getContentInfo(this.params.id);    //初始获取数据
    },
    mounted() {
      let clientHeight = document.body.clientHeight
      $('.cms-body').css('minHeight', clientHeight - 110 + 'px')
    },
    watch: {
      info: {
        handler(val) {
          if (val) {
            this.changeCount++
          }
        },
        deep: true
      }
    },
    beforeRouteLeave(to, from, next) {
      // 判断数据是否修改，如果修改按这个执行，没修改，则直接执行离开此页面
      if (this.isNoSaveLeave && this.changeCount > 0) {
        this.$confirm("您填写的内容未保存，确定离开吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          next();
        });
      } else {
        next()
      }
    }
  };
</script>

<style scss scoped>
  .content-inner-box {
    width: 400px;
    position: relative;
    left: -145px;
  }

  .inner-label {
    display: inline-block;
    text-align: right;
    width: 95px;
    margin-right: 22px;
  }
</style>
